import http from '@/utils/http'
const qs = require("qs");
import { configStore } from "@/store/config";
const configUserStore = configStore();
import {formatHgaTime, utf16to8, replaceTeamName, formatTime, formatRECount, formatHttpUrl} from "@/utils/utils";


const domainURL = formatHttpUrl(configUserStore.setting.ps_url);

// export const getDataAPI = (params) => http.get('/api/data', params)


let formatGameListData = function (res, sys_time) {
	let list = [];
	try{
		/// 未开赛
		if(res['l'][0][2]) {
			 list = [...list, ...res['l'][0][2]];
		}
	}catch (e) {}
	try{
		/// 开赛中
		if(res['n'][0][2]) {
			list = [...list ,...res['n'][0][2]];
		}
	}catch (e) {}


	let gameList = [];

	list.forEach((item) => {
		let lid = item[0];
		let league = item[1];
		let teamInfoList = item[2];
		teamInfoList.forEach((citem) => {
			let score_h_c = "0-0";
			let ecid = citem[0];
			let handicap = citem[8]; // 盘口 0 让球盘， 1 大小盘，2 输赢盘

			citem[1] = replaceTeamName(citem[1]);
			citem[2] = replaceTeamName(citem[2]);

			try{
				if(citem[9]) {
					score_h_c = citem[9].join("-"); // 比分
				}
			}catch (e) {}

			let diff_time = 0;

			diff_time = ~~((citem[4] - sys_time)/1000);

			let tempItem = {};
			tempItem['competition'] = {
				'time_str': formatTime(citem[4]),
				'datetime_int': citem[4],
				"systime": formatTime(sys_time),
				"lid": lid,
				"gid": ecid,
				"league": league,
				"diff_time": diff_time,
				"diff_time_type":  (diff_time <= 0) ? "doing" : "warn",
				"ecid": ecid,
				"ecName": `${citem[1]} VS ${citem[2]}`,
				"team_h": citem[1],
				"team_c": citem[2],
				"ecid_doing": ecid,
				'score_h_c': score_h_c
			};
			// 盘口 0 让球盘， 1 大小盘，2 输赢盘
			for(let i=0;i<2;i++) {
				let tempHandicap = handicap[i];
				let tempKey = "";
				if(i === 1) {
					tempKey = "r_";
				}
				let num = i; // 表示第几局，前面KEY，0，1, 0全场，1上半场
				if(!tempHandicap) {
					continue;
				}
				try{
					if(tempHandicap[0][0]) {
						tempItem[tempKey + "concede_points"] = {
							"betHdp_h": tempHandicap[0][0][1] * 1,
							"betHdp_c": tempHandicap[0][0][0] * 1,
							"odds_h": tempHandicap[0][0][3] * 1,
							"odds_c": tempHandicap[0][0][4] * 1,
						}
					}
					if(tempHandicap[1][0]) {
						tempItem[tempKey + "min_big"] = {
							"betHdp_h": tempHandicap[1][0][1] * 1,
							"betHdp_c": tempHandicap[1][0][1] * 1,
							"odds_h": tempHandicap[1][0][2] * 1,
							"odds_c": tempHandicap[1][0][3] * 1,
						};
					}

				}catch (e){
				}
			}
			gameList.push(tempItem);
		})
	})

	gameList = gameList.sort(function (a, b) {
		if(a['competition']['datetime_int'] === b['competition']['datetime_int']) {
			return 0;
		}
		return a['competition']['datetime_int'] > b['competition']['datetime_int'] ? 1 : -1;
	})

	return gameList;
}

/// 登录
export const partLogin = async function (data) {
	let has_login = await http.post(domainURL + '/member-service/v2/login?locale=zh_CN', qs.stringify(data))
	if(has_login > 0) {
		return {
			"state": 1,
			'message': "login suc"
		}
	}
	return {
		"state": 0,
		'message': "login error"
	}
};
/// 获取账号余额
export const getParAccountBalance = async function () {
	let res = await http.get(domainURL + "/member-service/v2/account-balance?locale=zh_CN");
	if(res['loginId']) {
		return {
			'state': 1,
			'data': res,
			'message': 'getUserInfo suc'
		}
	}
	return {
		'state': 0,
		'message': 'getUserInfo error'
	}
}
/// 获取数据
export const loadParDataList = async function ($mk = 1, $sp = 29) {
	let timestamp = +new Date();
	let params = {
		"_g": "1",
		"btg": "1",
		"c": "",
		"cl": "3",
		"d": "",
		"ev": "",
		"g": "",
		"hle": "false",
		"inl": "false",
		"l": "1", /// tg 写3
		"lg": "",
		"lv": "",
		"me": "0",
		"mk": $mk,
		"more": "false",
		"o": "1",
		"ot": "2",
		"pa": "0",
		"pimo": "",
		"pn": "-1",
		"sp": $sp,
		"tm": "0",
		"v": "0",
		"wm": "",
		"locale": "zh_CN",
		"_": timestamp,
	};
	let _params = qs.stringify(params);
	let res =  await http.get(domainURL + "/sports-service/sv/odds/events?" + _params);
	if(!res) {
		return {
			'state': 0,
			'message': 'load gameList error'
		}
	}

	let sys_time =  0;
	try{
		sys_time = res.n[0][3];
	}catch (e){}

	if(sys_time === 0) {
		try{
			sys_time = res.l[0][3];
		}catch (e){}
	}else {
		try {
			if(res.l[0][3] > sys_time) {
				sys_time = res.l[0][3];
			}
		}catch (e) {

		}
	}

	let gameList = formatGameListData(res, sys_time);
	return {
		'state': 1,
		'data': {
			gameList: gameList,
			sys_time: sys_time
		}
	}
}
